package com.wuseguang.springcloud.service.impl;

import ch.qos.logback.classic.spi.LoggerComparator;
import com.wuseguang.springcloud.dao.jdbc.ExaminationRoomJdbc;
import com.wuseguang.springcloud.dao.jdbc.ExamsJdbc;
import com.wuseguang.springcloud.dao.repository.ExaminationRoomRepository;
import com.wuseguang.springcloud.dao.repository.ExamsRepository;
import com.wuseguang.springcloud.entity.Exams;
import com.wuseguang.springcloud.service.ExamsService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.UpdateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-28
 */
@Service
public class ExamsServiceImpl  implements ExamsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private ExaminationRoomRepository examinationRoomRepository;
    @Autowired
    private ExamsRepository examsRepository;
    @Autowired
    private ExamsJdbc examsJdbc;

    @Override
    public Result deleteExamsById(Integer id) {
        logger.info("删除考试信息");
        try {
            examsRepository.deleteById(id);
        } catch (Exception e) {

        }
        return new Result();
    }

    @Override
    public Result addExams(Exams exams) {
        logger.info("添加考试信息");
        examsRepository.save(exams);
        return new Result();
    }

    @Override
    public Result updateExams(Exams exams) {
        logger.info("修改考试信息");
        //查询这个考试的旧数据
        Exams examsTwo = examsRepository.findById(exams.getId()).get();
        UpdateTool.copyNullProperties(examsTwo,exams);
        examsRepository.save(exams);
        return new Result();
    }

    @Override
    public Result findExamsByExaminationRoomId(Integer examinationRoomId, Integer indexNo, Integer rows) {

        logger.info("查询考场考试");
        //查考场考试
        String sql = "select e.id,e.classroom_id,e.examination_room_no," +
                "e.grade_id,e.course_id,e.teacher_id,date_format(e.start_time,'%Y-%m-%d %T') start_time, " +
                "date_format(e.end_time,'%Y-%m-%d %T') end_time,e.examination_room_id,e.school_id,date_format(e.add_time,'%Y-%m-%d %T') add_time"+
                ",cr.`name` classroomName,g.`name` gradeName,cs.`name` courseName,e.seat_url ,e.grade_binding," +
                "e.techer_binding,er.name  examinationRoomName from exams e " +
                "left join classroom cr on(cr.id=e.classroom_id) " +
                "LEFT JOIN grade g ON ( g.id = e.grade_id ) " +
                "left join course cs on(cs.id=e.course_id) " +
                "left join examination_room er on(er.id=e.examination_room_id) " +
                "where e.examination_room_id =? ORDER BY e.classroom_id desc";
        List<Map<String,Object>> list = examsJdbc.queryListMap(sql,examinationRoomId);

        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(),rows, tool.getList(), indexNo));

    }


}
